﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FunDraw.LayerInfos;
using FunDraw.usercontrols;

namespace FunDraw
{
    public partial class Form1 : Form
    {
        public LAYERTYPE curLayerType = LAYERTYPE.POINT;
        public BaseEquation curEquation = null;
        public MyDrawLayer curDrawLayer = null;
        /// <summary>
        /// 工具卡页列表
        /// </summary>
        public List<TabPage> toolPageList = new List<TabPage>();

        public Form1()
        {
            InitializeComponent();
            layerlistview.OnValueChange += new EventHandler(listItemChange);
            //将卡页全部收集到列表中
            makeToolList();
        }
        /// <summary>
        /// 生成工具列表
        /// </summary>
        private void makeToolList()
        {
            for(int i=0;i<=tabControl1.TabPages.Count-1;i++)
            {
                toolPageList.Add(tabControl1.TabPages[i]);
            }
        }



        private void coordinate1_Resize(object sender, EventArgs e)
        {
            coordinate1.Invalidate();
        }

        /// <summary>
        /// 调整坐标系背景颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SelBackColor_Click(object sender, EventArgs e)
        {
            if(colordlg.ShowDialog()==DialogResult.OK)
            {
                btn_SelBackColor.BackColor = colordlg.Color;
                coordinate1.BackColor = colordlg.Color;
                RefreshLayer();
            }
        }
        /// <summary>
        /// 调整坐标系线条颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SelLineColor_Click(object sender, EventArgs e)
        {
            if (colordlg.ShowDialog() == DialogResult.OK)
            {
                (sender as Button).BackColor = colordlg.Color;
                coordinate1.AxisLineColor = colordlg.Color;
                RefreshLayer();
            }
        }
        /// <summary>
        /// 调整坐标信息显示颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SelInfoColor_Click(object sender, EventArgs e)
        {
            if (colordlg.ShowDialog() == DialogResult.OK)
            {
                (sender as Button).BackColor = colordlg.Color;
                coordinate1.InfoColor = colordlg.Color;
                RefreshLayer();
            }
        }

        /// <summary>
        /// 选择标记字体样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SelTagFont_Click(object sender, EventArgs e)
        {
            if(fontDialog.ShowDialog()==DialogResult.OK)
            {
                btn_SelTagFont.Text = string.Format("{0},{1},{2}", fontDialog.Font.Name.ToString(),
                    fontDialog.Font.Size, fontDialog.Font.Style.ToString());
                coordinate1.TagFont = fontDialog.Font;
                RefreshLayer();
            }
        }
        /// <summary>
        /// 调整每刻度代表的像素点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numspan_unit_ValueChanged(object sender, EventArgs e)
        {
            coordinate1.Unit = (int)Math.Floor(numspan_unit.Value);
            RefreshLayer();
        }
        /// <summary>
        /// 调整坐标系四周留白
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numspan_bound_ValueChanged(object sender, EventArgs e)
        {
           coordinate1.Bound = (int)Math.Floor(numspan_bound.Value);
            RefreshLayer();
        }

        /// <summary>
        /// 显示或隐藏图层工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chk_showlayer_CheckedChanged(object sender, EventArgs e)
        {
            pnl_layerTools.Visible = chk_showlayer.Checked;
        }

        private void coordinate1_Load(object sender, EventArgs e)
        {

        }

        private void myDrawLayer1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 显示坐标器背景网格线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chk_showgrid_CheckedChanged(object sender, EventArgs e)
        {
            coordinate1.GridLine = chk_showgrid.Checked;
            RefreshLayer();
        }

        /// <summary>
        /// 变更线条颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_gridline_Click(object sender, EventArgs e)
        {
            if (colordlg.ShowDialog() == DialogResult.OK)
            {
                btn_gridline.BackColor = colordlg.Color;
                coordinate1.GridLineColor = colordlg.Color;
                RefreshLayer();
            }
        }
        /// <summary>
        /// 背景单元格大小调整
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// 图层列表选中值变化的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listItemChange(object sender,EventArgs e)
        {
            try
            {
                ListViewItem item = (sender as ListView).SelectedItems[0];
                string currentlayname = item.SubItems[2].Text;
                lbl_layername.Text = currentlayname;
                string ltype = item.SubItems[1].Text;
                string id = item.SubItems[3].Text;
                ShowToolGroupByLayerType(ltype);
                curEquation = layerlistview.GetEquationByID(id);
                curDrawLayer = layerlistview.GetDrawLayerByID(id);
            }
            catch
            {

            }
        }
        /// <summary>
        /// 根据不同的图层类别显示工具组
        /// </summary>
        /// <param name="lyt">图层枚举类型名称</param>
        private void ShowToolGroupByLayerType(string lyt)
        {

            for (int i = 0; i <= toolPageList.Count - 1; i++)
            {
                TabPage tp = toolPageList[i];
                string tabname = tp.Tag==null?"":tp.Tag.ToString();
                if (tabname == "main" || tabname==lyt)
                {
                    tp.Parent = tabControl1;
                }
                else
                {
                    tp.Parent = tab_hine;
                }
            }
            if(tabControl1.TabPages.Count>1)
                tabControl1.SelectedIndex = 1;

        }

        private void chk_quadranttag_CheckedChanged(object sender, EventArgs e)
        {
            coordinate1.ShowQuadrant = chk_quadranttag.Checked;
            RefreshLayer();
        }
   
        /// <summary>
        /// 坐标系尺寸变换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void coordinate1_SizeChanged(object sender, EventArgs e)
        {
            coordinate1.Invalidate();
            //刷新图层绘制
            RefreshLayer();
        }
        /// <summary>
        /// 刷新图层的绘制
        /// </summary>
        private void RefreshLayer()
        {
            LayerSize lys = new LayerSize(coordinate1.Unit, coordinate1.Bound, coordinate1.Height, coordinate1.Width);

            layerlistview.RefreshDrawLayer(lys);
        }


        /// <summary>
        /// 增加新的图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_newlayer_Click(object sender, EventArgs e)
        {
            DlgNewLayer dnl = new DlgNewLayer();
            if (dnl.ShowDialog() == DialogResult.OK)
            {
                LAYERTYPE lt = dnl.LayerType;
                string lname = dnl.LayerName;
                LayerSize lys = new LayerSize(coordinate1.Unit, coordinate1.Bound, coordinate1.Height, coordinate1.Width);

                layerlistview.appendNewDrawLayer(lt, pnl_center, lys,new MouseEventHandler(LayerMouseMove), lname);

            }
        }
        /// <summary>
        /// 图层鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void LayerMouseMove(object sender, MouseEventArgs e)
        {
            Point p = new Point(e.X, e.Y);
            coordinate1.UpdateMousePostion(p);

        }
        /// <summary>
        /// 显示图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_showlayer_Click(object sender, EventArgs e)
        {
            if (!layerlistview.checkHaveCheck())
                MessageBox.Show("请选中待操作的图层");
            else
            {
                layerlistview.changeVisableLayer(true);
            }
        }
        /// <summary>
        /// 删除选中图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_delLayer_Click(object sender, EventArgs e)
        {
            if (!layerlistview.checkHaveCheck())
                MessageBox.Show("请先选中待删除的图层!");
            else
            {
                DialogResult dr = MessageBox.Show("确认删除选中图层吗?", "删除确认", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    layerlistview.deleteSelected();
                    int itemcount = layerlistview.GetItemCount();
                    if(itemcount==0)
                    {
                        ShowToolGroupByLayerType("main");
                    }
                }
               
            }
        }

        private void pnl_center_Resize(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 重命名图层名称
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_renamelayer_Click(object sender, EventArgs e)
        {
            DlgRenameLayer dl = new DlgRenameLayer();
            DialogResult dr = dl.ShowDialog();
            if(dr==DialogResult.OK)
            {
                layerlistview.RnameLayer(dl.layername);
            }
        }


        /// <summary>
        /// 隐藏图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_disable_Click(object sender, EventArgs e)
        {
            if (!layerlistview.checkHaveCheck())
                MessageBox.Show("请选中待操作的图层");
            else
            {
                layerlistview.changeVisableLayer(false);
            }
        }

        /// <summary>
        /// 上移选中图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_moveup_Click(object sender, EventArgs e)
        {
            layerlistview.moveUpSelected();
        }

        /// <summary>
        /// 下移选中图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_movedown_Click(object sender, EventArgs e)
        {
            layerlistview.moveDownSelected();
        }

        /// <summary>
        /// 窗体展现事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Shown(object sender, EventArgs e)
        {
            ShowToolGroupByLayerType("main");
        }

        /// <summary>
        /// 直线方程线条颜色变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_LINE_color_Click(object sender, EventArgs e)
        {
            if(colordlg.ShowDialog()==DialogResult.OK)
            {
                
                if (curDrawLayer != null)
                {
                    btn_LINE_color.BackColor = colordlg.Color;
                    curEquation.LineColor= colordlg.Color;
                    curDrawLayer.Invalidate();
                }
            }
        }
        /// <summary>
        /// 更改标直线方程的标记颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_LINE_tagcolor_Click(object sender, EventArgs e)
        {
            if (colordlg.ShowDialog() == DialogResult.OK)
            {
                
                if (curDrawLayer != null)
                {
                    btn_LINE_tagcolor.BackColor = colordlg.Color;
                    curEquation.TagColor = colordlg.Color;
                    curDrawLayer.Invalidate();
                    curDrawLayer.Refresh();     
                }
            }
        }

        private void chk_LINE_showtag_CheckedChanged(object sender, EventArgs e)
        {
            if(curDrawLayer!=null)
            {
                curDrawLayer.Visible = false;
                curEquation.ShowTag = chk_LINE_showtag.Checked;
                curDrawLayer.Invalidate();
                curDrawLayer.Visible = true;
                btn_LINE_tagcolor.Enabled = chk_LINE_showtag.Checked;
            }
        }
        /// <summary>
        /// 变更方程A参数的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void num_LINE_A_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                float a = (float)num_LINE_A.Value;
       
                if(curEquation!=null)
                {
                    curDrawLayer.Visible = false;
                    (curEquation as LineEquation).A = a;
                    curDrawLayer.Invalidate();
                    lbl_runEquation.Text = curEquation.toString();
                    curDrawLayer.Visible = true;
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 更改直线方程B参数的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void num_LINE_B_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                float b = (float)num_LINE_B.Value;

                if (curEquation != null)
                {
                    curDrawLayer.Visible = false;
                    (curEquation as LineEquation).B = b;
                    curDrawLayer.Invalidate();
                    lbl_runEquation.Text = curEquation.toString();
                    curDrawLayer.Visible = true;
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 直线方程变更C参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void num_LINE_C_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                float c = (float)num_LINE_C.Value;
                if (curEquation != null)
                {
                    curDrawLayer.Visible = false;
                    (curEquation as LineEquation).C = c;
                    curDrawLayer.Invalidate();
                    lbl_runEquation.Text = curEquation.toString();
                    curDrawLayer.Visible = true;
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 变更直线方程的线宽
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void num_LINE_width_ValueChanged(object sender, EventArgs e)
        {
            try
            {

                int w = (int)num_LINE_width.Value;
                if (curEquation != null)
                {
                    curDrawLayer.Visible = false;
                    (curEquation as LineEquation).LineWidth = w;
                    curDrawLayer.Invalidate();
                    curDrawLayer.Visible = true;
                }
            }
            catch
            {

            }
        }
    }
}
